package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.*;
import com.hzhc.enumBean.DdEnum;
import com.hzhc.enumBean.FyEnum;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.YwOrderTypeEnum;
import com.hzhc.mapper.*;
import com.hzhc.service.*;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.vo.ChargeItemVo;
import com.hzhc.vo.DeclareCarVo;
import com.hzhc.vo.VehicleReservationVo;
import com.hzhc.vo.YwOrderCntrVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 卡口记录 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-30
 */
@Service
@RequiredArgsConstructor
public class DeclareCarServiceImpl extends ServiceImpl<DeclareCarMapper, DeclareCar> implements DeclareCarService {
    private final YwOrderCntrService ywOrderCntrService;
    private final YwOrderCntrMapper ywOrderCntrMapper;
    private final VehicleReservationService vehicleReservationService;
    private final VehicleReservationMapper vehicleReservationMapper;
    private final YwOrderMapper ywOrderMapper;
    private final CarInfoMapper carInfoMapper;
    private final CntrLogService cntrLogService;
    private final CntrLogMapper cntrLogMapper;
    private final CntrInfoMapper cntrInfoMapper;
    private final SysUserMapper sysUserMapper;
    private final ChargeConfigServiceImpl configService;
    private final ChargeConfigService chargeConfigService;
    private final ChargeContractMapper chargeContractMapper;
    private final CntrChargeDetailService cntrChargeDetailService;
    private final ChargeItemService chargeItemService;
    private final CompanyInfoMapper companyInfoMapper;
    private final TrainPlanDetailMapper trainPlanDetailMapper;

    private final TrainPlanDetailMapper detailMapper;
    private final PushService pushService;
    private final YwOrderCarMapper ywOrderCarMapper;

    @Override
    public List<DeclareCarVo> getDeclareCarList(DeclareCarVo vo, Page<DeclareCarVo> pagination) {
        IPage<DeclareCarVo> pageList = null;
        pageList = baseMapper.getDeclareCarPage(pagination, vo);
        Map<String, String> orderTypeMap = YwOrderTypeEnum.stream()
                .collect(Collectors.toMap(YwOrderTypeEnum::getCode, YwOrderTypeEnum::getName));
        for (DeclareCarVo declareCarVo : pageList.getRecords()) {
            YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", vo.getOrderNo()).eq("cntr_no", vo.getCntrA()).eq("disable", 0));
            YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", vo.getOrderNo()).eq("cntr_no", vo.getCntrB()).eq("disable", 0));
            if (cntrA != null) {
                declareCarVo.setGoodsA(cntrA.getGoodsName());
            }
            if (cntrB != null) {
                declareCarVo.setGoodsB(cntrB.getGoodsName());
            }
            if (orderTypeMap.containsKey(declareCarVo.getYwcode())) {
                declareCarVo.setOrderTypeName(orderTypeMap.get(declareCarVo.getYwcode()));
            }

            if((StrUtil.isNotBlank(declareCarVo.getTxOrderNo()) && declareCarVo.getTxOrderNo().indexOf("TZHK")!=-1) ||
                    (StrUtil.isNotBlank(declareCarVo.getOrderNo()) && declareCarVo.getOrderNo().indexOf("TZHK")!=-1)){
                if(declareCarVo.getYwcode().equals("KJ")){
                    declareCarVo.setOrderTypeName("提重回空(回空)");
                }else{
                    declareCarVo.setOrderTypeName("提重回空(提重)");
                }
            }
        }
        return pageList.getRecords();
    }

    @Override
    public List<YwOrderCntrVo> getGateYwOrderCntrList(DeclareCarVo vo) {
        if(vo.getOrderNoList().size()>0){
            vo.setOrderNoList(vo.getOrderNoList().stream().filter(s -> s.indexOf("HK")!=-1 || s.indexOf("SGJ")!=-1 || s.indexOf("TZHK")!=-1
                    || s.indexOf("ZJ")!=-1 || s.indexOf("KJ")!=-1 || s.indexOf("TKZJ")!=-1).collect(Collectors.toList()));
        }
        List<YwOrderCntrVo> gateYwOrderCntrList = ywOrderCntrMapper.getGateYwOrderCntrList(vo);
//        //提空重进时 选箱子 把箱子改成重箱显示
        if(StringUtils.isNotEmpty(vo.getOrderNo()) && vo.getOrderNo().indexOf(YwOrderTypeEnum.SGJ.getCode())!=-1){
            for (YwOrderCntrVo ywOrderCntrVo : gateYwOrderCntrList) {
                if(ywOrderCntrVo.getTallyState()==5 && YwOrderTypeEnum.SGJ.getCode().equals(ywOrderCntrVo.getOrderType())){
                    ywOrderCntrVo.setEfFlag("F");
                }
            }
        }
        return gateYwOrderCntrList;
    }

    @Override
    public List<DeclareCarVo> getOutGateYwOrderCntrList(DeclareCarVo vo) {
        List<DeclareCarVo> gateYwOrderCntrList = baseMapper.getOutGateYwOrderCntrList(vo);
        return gateYwOrderCntrList;
    }

    @Override
    public ResultUtil<T> addJkInGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = new DeclareCar();
        BeanUtils.copyProperties(vo, declareCar);
        declareCar.setCreateTime(LocalDateTime.now());
        declareCar.setPlanTime(LocalDateTime.now());
        declareCar.setDisable(0);
        VehicleReservation vehicleReservation = vehicleReservationMapper.selectById(vo.getVehicleId());
        vehicleReservation.setStatus(1);
        vehicleReservationMapper.updateById(vehicleReservation);
        List<CntrLog> cntrLogs=new ArrayList<>();
        if (vo.getYwOrderId() != null) {
            YwOrder ywOrder = ywOrderMapper.selectById(vo.getYwOrderId());
            // 查看车牌号是否存在不存在就新增
            List<CarInfo> carInfos = carInfoMapper
                    .selectList(new QueryWrapper<CarInfo>().eq("car_no", declareCar.getCarNo()));
            if (carInfos.isEmpty()) {
                CarInfo carInfo = new CarInfo();
                carInfo.setCarNo(declareCar.getCarNo());
                carInfo.setUserId(vo.getUserId());
                carInfo.setDisable(false);
                carInfo.setInoutCar(0);
                carInfoMapper.insert(carInfo);
            }
            // 修改清单表的预约状态
            YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrA()).eq("disable", 0));
            YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrB()).eq("disable", 0));
            List<YwOrderCntr> ywOrderCntrs = new ArrayList<>();
            if (cntrA != null) {
                cntrA.setDeclareFlag(1);
                cntrA.setJkWeight(vo.getJkWeight());
                cntrA.setSealNo(vo.getSealNoA());
                declareCar.setJzxIdA(cntrA.getId());
                ywOrderCntrs.add(cntrA);
                //集装箱进卡 保存日志
                CntrLog cntrLog = saveCntrLogMethod(vo,cntrA,vo.getXkA(),"集装箱进卡");
                cntrLogs.add(cntrLog);
            }
            if (cntrB != null) {
                cntrB.setDeclareFlag(1);
                cntrB.setJkWeight(vo.getJkWeight());
                cntrB.setSealNo(vo.getSealNoB());
                declareCar.setJzxIdB(cntrB.getId());
                ywOrderCntrs.add(cntrB);
                //集装箱进卡 保存日志
                CntrLog cntrLog = saveCntrLogMethod(vo,cntrB,vo.getXkB(),"集装箱进卡");
                cntrLogs.add(cntrLog);
            }

            if (!ywOrderCntrs.isEmpty()) {
                ywOrderCntrService.updateBatchById(ywOrderCntrs);
            }
            declareCar.setCompanyName(ywOrder.getCompanyName());
        }
        declareCar.setYwcode(vehicleReservation.getReservationType());
        boolean b = save(declareCar);
        cntrLogService.saveBatch(cntrLogs);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateDeclareCar(DeclareCarVo vo) {
        DeclareCar declareCar = getById(vo.getId());
        BeanUtils.copyProperties(vo,declareCar);
        if(!Objects.equals(vo.getVehicleId(), declareCar.getVehicleId())){
            VehicleReservation vehicleReservation = vehicleReservationMapper.selectById(declareCar.getVehicleId());
            vehicleReservation.setStatus(0);
            vehicleReservationMapper.updateById(vehicleReservation);
            VehicleReservation vehicleReservation1 = vehicleReservationMapper.selectById(vo.getVehicleId());
            vehicleReservation1.setStatus(1);
            vehicleReservationMapper.updateById(vehicleReservation1);
        }
        List<CntrLog> oldCntrlogs=new ArrayList<>();
        List<CntrLog> cntrLogs=new ArrayList<>();
        // 修改清单表的预约状态
        // 旧的卡口登记信息
        List<YwOrderCntr> oldYwOrderCntrs = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", declareCar.getOrderNo())
                .and(wrapper -> wrapper.eq("cntr_no", declareCar.getCntrA()).or().eq("cntr_no", declareCar.getCntrB()))
                .eq("disable", 0));

        // 新的卡口登记信息
        List<YwOrderCntr> newYwOrderCntrs = ywOrderCntrMapper
                .selectList(new QueryWrapper<YwOrderCntr>().eq("order_no", declareCar.getOrderNo())
                        .and(wrapper -> wrapper.eq("cntr_no", vo.getCntrA()).or().eq("cntr_no", vo.getCntrB()))
                        .eq("disable", 0));

        for (YwOrderCntr trainPlanCntr : oldYwOrderCntrs) {
            trainPlanCntr.setDeclareFlag(0);
            //集装箱进卡 保存日志
            CntrLog cntrLog = cntrLogMapper.selectOne(new QueryWrapper<CntrLog>().eq("disable", 0)
                    .eq("order_no", trainPlanCntr.getOrderNo()).eq("cntr_no",trainPlanCntr.getCntrNo()).eq("remark", "集装箱进卡"));
            oldCntrlogs.add(cntrLog);
        }

        for (YwOrderCntr trainPlanCntr : newYwOrderCntrs) {
            trainPlanCntr.setDeclareFlag(1);
            String damagedFlag = "";
            if (trainPlanCntr.getCntrNo().equals(vo.getCntrA())){
                damagedFlag = vo.getXkA();
            }else if (trainPlanCntr.getCntrNo().equals(vo.getCntrB())){
                damagedFlag = vo.getXkA();
            }
            //集装箱进卡 保存日志
            CntrLog cntrLog = saveCntrLogMethod(vo, trainPlanCntr,damagedFlag,"集装箱进卡");
            cntrLogs.add(cntrLog);
        }
        boolean b = updateById(declareCar);
        //清楚旧日志 保存新日志
        for (CntrLog cntrLog : oldCntrlogs) {
            cntrLog.setDisable(1);
        }
        cntrLogService.updateBatchById(cntrLogs);
        cntrLogService.saveBatch(cntrLogs);

        if (!oldYwOrderCntrs.isEmpty()) {
            ywOrderCntrService.updateBatchById(oldYwOrderCntrs);
        }
        if (!newYwOrderCntrs.isEmpty()) {
            ywOrderCntrService.updateBatchById(newYwOrderCntrs);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteDeclareCar(int id) {
        DeclareCar declareCar = getById(id);

        if(declareCar.getTallyFlag()==10 || declareCar.getTallyFlagB()==10){
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_TALLY_YET);
        }
        if(declareCar.getOutType()==1){
            return ResultUtil.build(ResultUtilCodeEnum.CAR_OUT_YET);
        }
        declareCar.setDisable(1);

        boolean b = updateById(declareCar);
        DeclareCar declareCar1 = baseMapper.selectOne(new QueryWrapper<DeclareCar>().eq("pid", declareCar.getId()).eq("disable", 0));
        if(declareCar1!=null){
            declareCar1.setDisable(1);
            updateById(declareCar1);
        }
        List<YwOrderCntr> trainPlanCntrs=new ArrayList<>();

        List<CntrLog> cntrLogs=new ArrayList<>();

        String cntrA=declareCar.getCntrA();
        String cntrB=declareCar.getCntrB();

        if(declareCar.getVehicleId()==null && declareCar.getVehicleIdB()==null){
            trainPlanCntrs = ywOrderCntrMapper
                    .selectList(new QueryWrapper<YwOrderCntr>()
                            .and(wrapper -> wrapper.eq("cntr_no", cntrA).or().eq("cntr_no", cntrB)).eq("disable", 0));
        }

        for (YwOrderCntr trainPlanCntr : trainPlanCntrs) {
            trainPlanCntr.setDeclareFlag(0);
            //集装箱进卡 保存日志
            List<CntrLog> cntrLog = cntrLogMapper.selectList(new QueryWrapper<CntrLog>().eq("disable", 0)
                    .eq("order_no", trainPlanCntr.getOrderNo()).eq("cntr_no",trainPlanCntr.getCntrNo()).eq("remark", "集装箱进卡"));
            cntrLogs.addAll(cntrLog);
        }
        if (!trainPlanCntrs.isEmpty()) {
            ywOrderCntrService.updateBatchById(trainPlanCntrs);
        }
        for (CntrLog cntrLog : cntrLogs) {
            cntrLog.setRemark("集装箱进卡记录删除");
        }
        cntrLogService.saveBatch(cntrLogs);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public List<DeclareCarVo> getZkTodayRcCarList(Page<DeclareCarVo> page, DeclareCarVo dvo) {
        IPage<DeclareCarVo> volist = baseMapper.getZkTodayCarList(page,dvo);
        Map<String, String> orderTypeMap = YwOrderTypeEnum.stream()
                .collect(Collectors.toMap(YwOrderTypeEnum::getCode, YwOrderTypeEnum::getName));
        for (DeclareCarVo vo : volist.getRecords()) {
            // 检查Map中是否有对应的orderType
            if (orderTypeMap.containsKey(vo.getOrderType())) {
                vo.setOrderTypeName(orderTypeMap.get(vo.getOrderType()));
            }
            QueryWrapper<YwOrderCntr> qwA = new QueryWrapper<YwOrderCntr>();
            qwA.eq("order_no", vo.getOrderNo());
            qwA.eq("cntr_no", vo.getCntrA());
            qwA.eq("disable", 0);
            YwOrderCntr ywOrderCntrA = ywOrderCntrMapper.selectOne(qwA);
            if (ywOrderCntrA != null) {
                vo.setCntrSizeA(ywOrderCntrA.getCntrSize());
                vo.setYwOrderCntrIdA(ywOrderCntrA.getId());
                vo.setEfFlagA(ywOrderCntrA.getEfFlag());
            }
            QueryWrapper<YwOrderCntr> qwB = new QueryWrapper<YwOrderCntr>();
            qwB.eq("order_no", vo.getOrderNo());
            qwB.eq("cntr_no", vo.getCntrB());
            qwB.eq("disable", 0);
            YwOrderCntr ywOrderCntrB = ywOrderCntrMapper.selectOne(qwB);
            if (ywOrderCntrB != null) {
                vo.setCntrSizeB(ywOrderCntrB.getCntrSize());
                vo.setYwOrderCntrIdB(ywOrderCntrB.getId());
                vo.setEfFlagB(ywOrderCntrB.getEfFlag());
            }
            CntrInfo cntrInfoA = cntrInfoMapper
                    .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", vo.getCntrA()));
            if (cntrInfoA != null) {
                vo.setBayA(cntrInfoA.getBay());
            } else {
                vo.setBayA(null);
            }
            CntrInfo cntrInfoB = cntrInfoMapper
                    .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", vo.getCntrB()));
            if (cntrInfoB != null) {
                vo.setBayB(cntrInfoB.getBay());
            } else {
                vo.setBayB(null);
            }
            if(YwOrderTypeEnum.SGJ.getCode().equals(vo.getOrderType()) && YwOrderTypeEnum.TK.getCode().equals(vo.getYwcode())){
                List<YwOrderCntr> ywOrderCntrs = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>().eq("order_no", vo.getOrderNo()).eq("disable", 0));
                if(ywOrderCntrs.size()>0){
                    List<String> collect = ywOrderCntrs.stream().filter(s -> StringUtils.isNotEmpty(s.getCntrNo())).map(YwOrderCntr::getCntrNo).collect(Collectors.toList());
                    vo.setCntrNoList(collect);
                }
            }
        }
        return volist.getRecords();
    }

    @Override
    public List<DeclareCarVo> getIpadTodayCarList(DeclareCarVo dvo) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String endPlanTime = df.format(time) + " 23:59:59";
        String startPlanTime = df.format(time) + " 00:00:00";
        LocalDateTime s1 = LocalDateTime.parse(startPlanTime, df1);
        LocalDateTime s2 = s1.plusHours(-12);
        // startPlanTime = df1.format(s2);
        dvo.setStartTime(s2);
        dvo.setEndTime(LocalDateTime.parse(endPlanTime, df1));
        List<DeclareCarVo> volist = baseMapper.getZkTodayCarList(dvo);
        for (DeclareCarVo vo : volist) {
            if(StrUtil.isBlank(vo.getJhBaya())){
                vo.setJhBaya(vo.getJhAreaa());
            }
            if(StrUtil.isBlank(vo.getJhBayb())){
                vo.setJhBayb(vo.getJhAreab());
            }
            QueryWrapper<YwOrderCntr> qwA = new QueryWrapper<YwOrderCntr>();
            qwA.eq("order_no", vo.getOrderNo());
            qwA.eq("cntr_no", vo.getCntrA());
            qwA.eq("disable", 0);
            YwOrderCntr ywOrderCntrA = ywOrderCntrMapper.selectOne(qwA);
            if (ywOrderCntrA != null) {
                vo.setCntrSizeA(ywOrderCntrA.getCntrSize());
                vo.setYwOrderCntrIdA(ywOrderCntrA.getId());
                vo.setEfFlagA(ywOrderCntrA.getEfFlag());
                vo.setTallyFlag(ywOrderCntrA.getTallyState());
            } else {
                vo.setYwOrderCntrIdA(null);
                vo.setCntrA(null);
                vo.setJhBaya(null);
                vo.setEfFlagA(null);
            }

            QueryWrapper<YwOrderCntr> qwB = new QueryWrapper<YwOrderCntr>();
            qwB.eq("order_no", vo.getOrderNo());
            qwB.eq("cntr_no", vo.getCntrB());
            qwB.eq("disable", 0);
            YwOrderCntr ywOrderCntrB = ywOrderCntrMapper.selectOne(qwB);
            if (ywOrderCntrB != null) {
                vo.setCntrSizeB(ywOrderCntrB.getCntrSize());
                vo.setYwOrderCntrIdB(ywOrderCntrB.getId());
                vo.setEfFlagB(ywOrderCntrB.getEfFlag());
                vo.setTallyFlagB(ywOrderCntrB.getTallyState());
            } else {
                vo.setCntrB(null);
                vo.setYwOrderCntrIdB(null);
                vo.setJhBayb(null);
                vo.setEfFlagB(null);
            }

            CntrInfo cntrInfoA = cntrInfoMapper
                    .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", vo.getCntrA()));
            if (cntrInfoA != null) {
                vo.setBayA(cntrInfoA.getBay());
            } else {
                vo.setBayA(null);
            }
            CntrInfo cntrInfoB = cntrInfoMapper
                    .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", vo.getCntrB()));
            if (cntrInfoB != null) {
                vo.setBayB(cntrInfoB.getBay());
            } else {
                vo.setBayB(null);
            }
        }
        return volist;
    }

    @Override
    public Integer getCountTodayRcCarList(String ywcode) {
        return baseMapper.getCountTodayRcCarList(ywcode);
    }


    @Override
    public ResultUtil<T> addCkInGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = baseMapper.selectById(vo.getId());
        vo.setCreateTime(declareCar.getCreateTime());
        vo.setPlanTime(declareCar.getPlanTime());
        vo.setOutType(1);
        vo.setOutTime(LocalDateTime.now());
        if (StrUtil.isBlank(vo.getCntrA())) {
            vo.setCntrA(declareCar.getCntrA());
        }
        if (StrUtil.isBlank(vo.getCntrB())) {
            vo.setCntrB(declareCar.getCntrB());
        }

        BeanUtils.copyProperties(vo, declareCar);
        int i = baseMapper.updateById(declareCar);
        VehicleReservation reservation = vehicleReservationMapper.selectById(declareCar.getVehicleId());
        reservation.setEndState(1);
        vehicleReservationMapper.updateById(reservation);
        YwOrder ywOrder = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable",0).eq("order_no",declareCar.getOrderNo()));
        YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrA()).eq("disable", 0));
        YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrB()).eq("disable", 0));
        // 修改清单表的预约状态
        List<YwOrderCntr> trainPlanCntrs = new ArrayList<>();
        if (cntrA != null) {
            cntrA.setOrderFlag(1);
            cntrA.setCkWeight(vo.getCkWeight());
            if(YwOrderTypeEnum.ZJ.getCode().equals(declareCar.getYwcode())){
                cntrA.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())
                        .subtract(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())).toString());
            }else{
                cntrA.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())
                        .subtract(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())).toString());
            }
            trainPlanCntrs.add(cntrA);
            ywOrderCntrMapper.updateById(cntrA);
        }
        if (cntrB != null) {
            cntrB.setOrderFlag(1);
            cntrB.setCkWeight(vo.getCkWeight());
            if(YwOrderTypeEnum.ZJ.getCode().equals(declareCar.getYwcode())){
                cntrB.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())
                        .subtract(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())).toString());
            }else{
                cntrB.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())
                        .subtract(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())).toString());
            }
            trainPlanCntrs.add(cntrB);
            ywOrderCntrMapper.updateById(cntrB);
        }
        if(i>0){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public ResultUtil<T> updateCntrTxTally(DeclareCarVo declareCarVo) {
        String cntrNo = "";
        if (StrUtil.isNotBlank(declareCarVo.getCntrA())) {
            cntrNo = declareCarVo.getCntrA();
        } else if (StrUtil.isNotBlank(declareCarVo.getCntrB())) {
            cntrNo = declareCarVo.getCntrB();
        }
        if (StrUtil.isBlank(cntrNo)) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTRNO_NULL);
        }
        CntrInfo cntrInfo = cntrInfoMapper.selectOne(Wrappers.<CntrInfo>lambdaQuery()
                .eq(CntrInfo::getCntrNo, cntrNo));
        if (cntrInfo == null) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOT_EXIST);
        }
        if (cntrInfo.getStatus() != 0) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOTIN_YARD);
        }
        //箱子区域贝位判断
        if (cntrInfo.getArea().contains("区")){

        }else{
            //判断箱子上方是否有箱子
            if (upCntrCheck(cntrInfo.getBay())) {
                return ResultUtil.build(400, "贝位" + cntrInfo.getBay() + "上面存在箱子,请检查");
            }
        }

        VehicleReservation reservation = vehicleReservationMapper.selectById(declareCarVo.getId());
        //空回的预约信息
        VehicleReservation khreservation = vehicleReservationMapper.selectOne(new LambdaQueryWrapper<VehicleReservation>()
                .eq(VehicleReservation::getVehicleId, reservation.getId()));
        YwOrderCntr ywOrderCntr = null;
        YwOrderCntr ywOrderAJzx = null;
        YwOrderCntr ywOrderBJzx = null;
        VehicleReservation oldVehicleReservation = null;
        VehicleReservation kholdVehicleReservation = null;
        //提空不回或提空回重情况下
        boolean cntrAFlag = false;//箱号和箱A匹配
        boolean cntrAEFlag = false;//箱A空
        boolean cntrBFlag = false;//箱号和箱B匹配
        boolean cntrBEFlag = false;//箱B空
        boolean hxFlag=false;
        if (YwOrderTypeEnum.TK.getCode().equals(reservation.getReservationType())) {
            if (!"E".equals(cntrInfo.getEfFlag())) {
                return ResultUtil.build(ResultUtilCodeEnum.EFFLAG_IS_ERROR);
            }
            List<YwOrderCntr> ywOrderCntrs  = ywOrderCntrMapper.selectList(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", reservation.getOrderNo())
                    .eq("disable", 0)
                    .eq("tally_state",0)
                    .eq("cntr_no",cntrNo));
//                    .last("and (cntr_no IS NULL or cntr_no ='' )"));
            for (YwOrderCntr yj:ywOrderCntrs){
                String c1 = cntrInfo.getCntrSize().substring(0, 1);
                String c2 = yj.getCntrSize().substring(0,1);
                if(c1.equals(c2)){
                    ywOrderCntr = yj;
                    break;
                }
            }
            //退2个箱子理货
            if(reservation.getCntrIdA()==null && reservation.getCntrIdB()==null){
                if(ywOrderCntr!=null){
                    //箱尺寸不匹配
                    if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                        return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                    }
                    ywOrderCntr.setCntrNo(cntrNo);
                    if(StrUtil.isNotBlank(declareCarVo.getCntrA())){
                        cntrAFlag=true;
                        reservation.setCntrIdA(ywOrderCntr.getId());
                        reservation.setCntrSize(cntrInfo.getCntrSize());
                    }
                    if(StrUtil.isNotBlank(declareCarVo.getCntrB())){
                        cntrBFlag=true;
                        reservation.setCntrIdB(ywOrderCntr.getId());
                        reservation.setCntrSizeB(cntrInfo.getCntrSize());
                    }
                }else {
                    return ResultUtil.build(ResultUtilCodeEnum.CNTR_IS_ERROR);
                }
                //退前一个箱子理货
            }else if(reservation.getCntrIdA() == null && reservation.getCntrIdB() == 10){
                if(ywOrderCntr!=null){
                    //箱尺寸不匹配
                    if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                        return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                    }
                    ywOrderCntr.setCntrNo(cntrNo);
                    if(StrUtil.isNotBlank(declareCarVo.getCntrA())){
                        cntrAFlag=true;
                        reservation.setCntrIdA(ywOrderCntr.getId());
                        reservation.setCntrSize(cntrInfo.getCntrSize());
                    }
                }else {
                    return ResultUtil.build(ResultUtilCodeEnum.CNTR_IS_ERROR);
                }
                //退后一个箱子理货
            }else if(reservation.getCntrIdB() == null && reservation.getTallyFlag() == 10) {
                if(ywOrderCntr!=null){
                    //箱尺寸不匹配
                    if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                        return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                    }
                    ywOrderCntr.setCntrNo(cntrNo);
                    if(StrUtil.isNotBlank(declareCarVo.getCntrB())){
                        cntrBFlag=true;
                        reservation.setCntrIdB(ywOrderCntr.getId());
                        reservation.setCntrSizeB(cntrInfo.getCntrSize());
                    }
                }else {
                    return ResultUtil.build(ResultUtilCodeEnum.CNTR_IS_ERROR);
                }
            }else {
                if (reservation.getCntrIdA() != null) {
                    ywOrderAJzx = ywOrderCntrService.getById(reservation.getCntrIdA());
                    if(ywOrderAJzx == null){
                        return ResultUtil.build(400, "预约数据已被删除，无法进行理货！");
                    }
                    if (cntrNo.equals(ywOrderAJzx.getCntrNo())) {
                        cntrAFlag = true;
                    }
                    if (StrUtil.isBlank(ywOrderAJzx.getCntrNo())) {
                        cntrAEFlag = true;
                    }
                }
                if (reservation.getCntrIdB() != null) {
                    ywOrderBJzx = ywOrderCntrService.getById(reservation.getCntrIdB());
                    if(ywOrderBJzx == null){
                        return ResultUtil.build(400, "预约数据已被删除，无法进行理货！");
                    }
                    if (cntrNo.equals(ywOrderBJzx.getCntrNo())) {
                        cntrBFlag = true;
                    }
                    if (StrUtil.isBlank(ywOrderBJzx.getCntrNo())) {
                        cntrBEFlag = true;
                    }
                }
                if (cntrAFlag) {//如果是箱A匹配
                    ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdA());
                    if (ywOrderCntr == null) {
                        return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                    }
                } else if (cntrBFlag) {//如果是箱B匹配
                    ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdB());
                    if (ywOrderCntr == null) {
                        return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                    }
                } else if (cntrAEFlag && cntrBEFlag) {//如果箱AB都空着
                    if (StrUtil.isNotBlank(declareCarVo.getCntrA())) {
                        ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdA());
                        if (ywOrderCntr == null) {
                            return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                        }
                        //箱尺寸不匹配
                        if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                            return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                        }
                        //如果箱A尺寸和箱主都匹配
                        if (checkCntrSize(ywOrderCntr, cntrInfo) ) {
                            //赋值箱号,箱A开始匹配
                            ywOrderCntr.setCntrNo(cntrNo);
                            cntrAFlag = true;
                        }
                    }else if (StrUtil.isNotBlank(declareCarVo.getCntrB())) {
                        ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdB());
                        if (ywOrderCntr == null) {
                            return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                        }
                        if (checkCntrSize(ywOrderCntr, cntrInfo) ) {
                            //赋值箱号,箱B开始匹配
                            ywOrderCntr.setCntrNo(cntrNo);
                            cntrBFlag = true;
                        }
                    }
                } else if (cntrAEFlag) {//如果箱A空着
                    //赋值箱号,箱A开始匹配
                    ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdA());
                    if (ywOrderCntr == null) {
                        return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                    }
                    //箱尺寸不匹配
                    if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                        return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                    }
                    ywOrderCntr.setCntrNo(cntrNo);
                    cntrAFlag = true;
                } else if (cntrBEFlag) {
                    ywOrderCntr = ywOrderCntrService.getById(reservation.getCntrIdB());
                    if (ywOrderCntr == null) {
                        return ResultUtil.build(400, "提箱业务下该箱号不存在！");
                    }
                    //箱尺寸不匹配
                    if (!checkCntrSize(ywOrderCntr, cntrInfo)) {
                        return ResultUtil.build(ResultUtilCodeEnum.CNTRSIZE_IS_ERROR);
                    }
                    ywOrderCntr.setCntrNo(cntrNo);
                    cntrBFlag = true;
                } else {
                    //如果箱号和箱AB都不匹配且箱AB都不是没有箱,表示选的箱子和预约箱对不上
                    return ResultUtil.build(400, "请选择对应的箱子进行提箱操作！");
                }
            }
        } else {
            //提重
            ywOrderCntr = ywOrderCntrService.getOne(Wrappers.<YwOrderCntr>lambdaQuery()
                    .eq(YwOrderCntr::getOrderNo, reservation.getOrderNo())
                    .eq(YwOrderCntr::getCntrNo, cntrNo));

            if (ywOrderCntr == null) {
                return ResultUtil.build(400, "请选择车辆预约对应委托下的箱子进行提箱操作！");
            } else {
                //箱子存在，判断这辆车是否预约了该箱子并且未理货的
                YwOrderCntr finalYwOrderCntr = ywOrderCntr;
                QueryWrapper <VehicleReservation> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("order_no", reservation.getOrderNo());
                queryWrapper.eq("car_no", reservation.getCarNo());
                queryWrapper.eq("disable", 0);
                if(finalYwOrderCntr.getOrderNo().contains("TZHK")){
                    queryWrapper.eq("ts_status", 0);
                }
                queryWrapper.and(wrapper -> wrapper.ne("tally_flag", 10)
                        .or().ne("tally_flag_b",10));
                queryWrapper.and(wrapper -> wrapper.eq("cntr_id_a", finalYwOrderCntr.getId())
                        .or().eq("cntr_id_b", finalYwOrderCntr.getId()));
                oldVehicleReservation = vehicleReservationMapper.selectOne(queryWrapper);
                if (oldVehicleReservation == null) {
                    return ResultUtil.build(400, "该车辆未预约该箱子,请选择该车预约过的箱子！");
                }
                kholdVehicleReservation =  vehicleReservationMapper.selectOne(new LambdaQueryWrapper<VehicleReservation>()
                       .eq(VehicleReservation::getVehicleId, oldVehicleReservation.getId())
                       .eq(VehicleReservation::getDisable, 0));
            }
            if (ywOrderCntr.getId().longValue() == reservation.getCntrIdA()) {
                cntrAFlag = true;
            }
            if (reservation.getCntrIdB() != null) {
                if (ywOrderCntr.getId().longValue() == reservation.getCntrIdB()) {
                    cntrBFlag = true;
                }
            }

            //如果是箱A和箱B不匹配
            if (!cntrAFlag && !cntrBFlag) {
                //换箱
                hxFlag=true;
                if (StrUtil.isNotBlank(declareCarVo.getCntrA())) {
                    //还原之前预约的箱子的预约信息
                    ywOrderAJzx = ywOrderCntrMapper.selectById(reservation.getCntrIdA());
                    ywOrderBJzx = ywOrderCntrMapper.selectById(reservation.getCntrIdB());
                    if(ywOrderAJzx != null){
                        ywOrderAJzx.setVehicleId(null);
                        ywOrderAJzx.setOrderFlag(0);
                    }else if(ywOrderBJzx != null){
                        ywOrderBJzx.setVehicleId(null);
                        ywOrderBJzx.setOrderFlag(0);
                    }

                    //删除理货箱子之前的车辆预约的信息防止出现重复数据
                    if(declareCarVo.getCntrA().equals(oldVehicleReservation.getCntrNo())){
                        oldVehicleReservation.setCntrIdA(null);
                        oldVehicleReservation.setCntrNo(null);
                        oldVehicleReservation.setCntrSize(null);
                        //清空提重空回预约的空回信息 的箱A信息
                        if(kholdVehicleReservation != null){
                            kholdVehicleReservation.setCntrIdA(null);
                            kholdVehicleReservation.setCntrNo(null);
                            kholdVehicleReservation.setCntrSize(null);
                        }
                    } else if(declareCarVo.getCntrB().equals(oldVehicleReservation.getCntrNoB())){
                        oldVehicleReservation.setCntrIdB(null);
                        oldVehicleReservation.setCntrNoB(null);
                        oldVehicleReservation.setCntrSizeB(null);
                        //清空提重空回预约的空回信息 的箱B信息
                        if(kholdVehicleReservation != null){
                            kholdVehicleReservation.setCntrIdB(null);
                            kholdVehicleReservation.setCntrNoB(null);
                            kholdVehicleReservation.setCntrSizeB(null);
                        }
                    }

                    reservation.setCntrIdA(ywOrderCntr.getId());
                    reservation.setCntrNo(cntrNo);
                    reservation.setCntrSize(cntrInfo.getCntrSize());

                    //同时修改空回的预约信息
                    if(khreservation!=null){
                        khreservation.setCntrIdA(ywOrderCntr.getId());
                        khreservation.setCntrNo(cntrNo);
                        khreservation.setCntrSize(cntrInfo.getCntrSize());
                    }

                    //箱子40尺要清除箱B数据
                    if ("4".equals(cntrInfo.getCntrSize().substring(0, 1))) {
                        reservation.setCntrNoB(null);
                        reservation.setTallyFlagB(0);
                        //同时修改空回的预约信息
                        if(khreservation!=null) {
                            khreservation.setCntrNoB(null);
                            khreservation.setTallyFlagB(0);
                        }
                    }
                    cntrAFlag = true;
                    ywOrderCntr.setVehicleId(reservation.getId());
                    ywOrderCntr.setOrderFlag(1);
                }else if (StrUtil.isNotBlank(declareCarVo.getCntrB())) {
                    //还原之前预约的箱子的预约信息
                    ywOrderBJzx = ywOrderCntrMapper.selectById(reservation.getCntrIdB());
                    ywOrderAJzx = ywOrderCntrMapper.selectById(reservation.getCntrIdA());
                    if(ywOrderBJzx != null) {
                        ywOrderBJzx.setVehicleId(null);
                        ywOrderBJzx.setOrderFlag(0);
                    }else if (ywOrderAJzx != null){
                        ywOrderAJzx.setVehicleId(null);
                        ywOrderAJzx.setOrderFlag(0);
                    }
                    //删除这理货箱子之前车辆预约的信息防止出现重复数据
                    if(declareCarVo.getCntrB().equals(oldVehicleReservation.getCntrNoB())){
                        oldVehicleReservation.setCntrIdB(null);
                        oldVehicleReservation.setCntrNoB(null);
                        oldVehicleReservation.setCntrSizeB(null);

                        //清空提重空回预约的空回信息 的箱B信息
                        if(kholdVehicleReservation != null){
                            kholdVehicleReservation.setCntrIdB(null);
                            kholdVehicleReservation.setCntrNoB(null);
                            kholdVehicleReservation.setCntrSizeB(null);
                        }

                    }else if(declareCarVo.getCntrA().equals(oldVehicleReservation.getCntrNo())){
                        oldVehicleReservation.setCntrIdA(null);
                        oldVehicleReservation.setCntrNo(null);
                        oldVehicleReservation.setCntrSize(null);

                        //清空提重空回预约的空回信息 的箱A信息
                        if(kholdVehicleReservation != null){
                            kholdVehicleReservation.setCntrIdA(null);
                            kholdVehicleReservation.setCntrNo(null);
                            kholdVehicleReservation.setCntrSize(null);
                        }
                    }

                    reservation.setCntrIdB(ywOrderCntr.getId());
                    reservation.setCntrNoB(cntrNo);
                    reservation.setCntrSizeB(cntrInfo.getCntrSize());

                    //同时修改空回的预约信息
                    if(khreservation!=null) {
                        khreservation.setCntrIdB(ywOrderCntr.getId());
                        khreservation.setCntrNoB(cntrNo);
                        khreservation.setCntrSizeB(cntrInfo.getCntrSize());
                    }
                    //箱子40尺要清除箱A数据
                    if ("4".equals(cntrInfo.getCntrSize().substring(0, 1))) {
                        reservation.setCntrNo(null);
                        reservation.setTallyFlag(0);
                        //同时修改空回的预约信息
                        if(khreservation!=null) {
                            khreservation.setCntrNo(null);
                            khreservation.setTallyFlag(0);
                        }
                    }
                    cntrBFlag = true;
                    ywOrderCntr.setVehicleId(reservation.getId());
                    ywOrderCntr.setOrderFlag(1);
                }
                if(oldVehicleReservation.getCntrIdA() == null && oldVehicleReservation.getCntrIdB() == null){
                    oldVehicleReservation.setDisable(1);
                }

                if(kholdVehicleReservation != null &&kholdVehicleReservation.getCntrIdA() == null && kholdVehicleReservation.getCntrIdB() == null){
                    kholdVehicleReservation.setDisable(1);
                }
            }

//           if( (!cntrAFlag) && (!cntrBFlag)){
//            if (reservation.getCntrIdA() == null) {
//                    //箱A为空,默认箱A装箱,卡口进卡没选箱子回写数据
//                    reservation.setCntrIdA(ywOrderCntr.getId());
//                    reservation.setCntrNo(cntrNo);
//                    reservation.setCntrSize(cntrInfo.getCntrSize());
//                    //箱子40尺要清除箱B数据
//                    if ("4".equals(cntrInfo.getCntrSize().substring(0, 1))) {
//                        reservation.setCntrNoB(null);
//                        reservation.setTallyFlagB(0);
//                    }
//                    cntrAFlag = true;
//                } else if (reservation.getCntrIdB() == null) {
//                    //箱B为空,默认箱B装箱,卡口进卡没选箱子回写数据
//                    reservation.setCntrIdB(ywOrderCntr.getId());
//                    reservation.setCntrNoB(cntrNo);
//                    reservation.setCntrSizeB(cntrInfo.getCntrSize());
//                    //箱子40尺要清除箱A数据
//                    if ("4".equals(cntrInfo.getCntrSize().substring(0, 1))) {
//                        reservation.setCntrNo(null);
//                        reservation.setTallyFlag(0);
//                    }
//                    cntrBFlag = true;
//                } else {
//                    //如果箱号和箱AB都不匹配且箱AB都不是没有箱,表示选的箱子和预约箱对不上
//                    return ResultUtil.build(400, "请选择对应的箱子进行提箱操作！");
//                }
//        }

        }
        if(ywOrderCntr == null){
            return ResultUtil.build(400, "箱清单信息不存在");
        }
        if (ywOrderCntr.getTallyState() != 0) {
            return ResultUtil.build(400, "箱子已理货,请勿重新操作");
        }
        //箱A40尺不允许继续理货
        if (StrUtil.isNotBlank(reservation.getCntrNo()) && reservation.getCntrSize().contains("4") && reservation.getTallyFlag().equals(10)) {
            return ResultUtil.build(400, "箱子" + reservation.getCntrNo() + reservation.getCntrSize() + ",无法继续理货");
        }
        //箱B40尺不允许继续理货
        if (StrUtil.isNotBlank(reservation.getCntrNoB()) && reservation.getCntrSizeB().contains("4") && reservation.getTallyFlagB().equals(10)) {
            return ResultUtil.build(400, "箱子" + reservation.getCntrNoB() + reservation.getCntrSizeB() + ",无法继续理货");
        }
        //已有箱子理货情况下,不能理40尺箱子
        if ("4".equals(cntrInfo.getCntrSize().substring(0, 1))) {
            if (StrUtil.isNotBlank(reservation.getCntrNo()) && reservation.getTallyFlag().equals(10)) {
                return ResultUtil.build(400, "箱子" + reservation.getCntrNo() + "已理货,只能理20尺箱子,无法继续理货");
            }
            if (StrUtil.isNotBlank(reservation.getCntrNoB()) && reservation.getTallyFlagB().equals(10)) {
                return ResultUtil.build(400, "箱子" + reservation.getCntrNoB() + "已理货,只能理20尺箱子,无法继续理货");
            }
        }
        //修改清单箱理货状态
        //业务中存在两次理货的数据,理货状态设置为第一次理货
        List<String> codes = Collections.singletonList(YwOrderTypeEnum.TK.getCode());
        if (codes.contains(reservation.getReservationType())) {
            ywOrderCntr.setTallyState(5);
            ywOrderCntr.setEfFlag("F");
        } else {
            ywOrderCntr.setTallyState(10);
        }
        //业务中存在两次理货的数据,第二次设置结束理货时间

        ywOrderCntr.setTallyTime(LocalDateTime.now());
        ywOrderCntr.setLhWeight(declareCarVo.getLhWeight());
        if (declareCarVo.getUserId() != null) {
            ywOrderCntr.setTallyUserId(declareCarVo.getUserId());
        }else if(declareCarVo.getMemberId() != null){
            ywOrderCntr.setTallyMemberId(declareCarVo.getMemberId());
        }
        ywOrderCntr.setTallyUserName(declareCarVo.getUserName());


        YwOrder ywOrder = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable",0).eq("order_no",ywOrderCntr.getOrderNo()));


        //判断箱子收货人 和 委托公司是否同一个
        if(StringUtils.isNotEmpty(cntrInfo.getConsignee())){
            CompanyInfo companyIdByName = companyInfoMapper.getCompanyIdByName(cntrInfo.getConsignee());
        if (companyIdByName !=null && !Long.valueOf(companyIdByName.getId()).equals(Long.valueOf(ywOrder.getCompanyId()))){
                return ResultUtil.build(400, "集装箱收货人不匹配，无法提箱！");
            }
        }

        //修改箱信息表状态
        cntrInfo.setBay("");
        cntrInfo.setArea("");
        cntrInfo.setStatus(1);
        cntrInfo.setTrackOrCar(1);//汽车
        cntrInfo.setRemark("提箱理货");
        cntrInfo.setOperateType("TXLH");
        cntrInfo.setUserId(declareCarVo.getUserId());
        cntrInfo.setMemberId(declareCarVo.getMemberId());
        cntrInfo.setUserName(declareCarVo.getUserName());
        cntrInfo.setAccesspath(declareCarVo.getAccesspath());
        cntrInfo.setCarNo(declareCarVo.getCarNo());
        if(ywOrder != null && YwOrderTypeEnum.SGJ.getCode().equals(ywOrder.getOrderType())){
            cntrInfo.setLogTag("0");
        }
        cntrInfo.setOrderNo(reservation.getOrderNo());
        cntrInfo.setCntrWorkRemarkB(DdEnum.YTC.getName());

        cntrInfo.setLockFlag(0);//散改集 提箱理货后解锁
        cntrInfo.setOutTime(LocalDateTime.now());
        cntrInfo.setLhWeight(declareCarVo.getLhWeight());
        //保存箱动态
        CntrLog cntrLog = new CntrLog();
        BeanUtils.copyProperties(cntrInfo, cntrLog);
        cntrLog.setVehicleReservationId(reservation.getId());
        cntrLog.setCreatetime(LocalDateTime.now());
        if(ywOrder != null && YwOrderTypeEnum.SGJ.getCode().equals(ywOrder.getOrderType())) {
            cntrLog.setLockRemark("散改集提箱理货解锁");
        }
        cntrLog.setId(null);

        boolean istallEnd=false;
        //修改进卡提箱理货状态
        if (cntrAFlag) {
            //箱A理货
            // 只存在一个箱子 2个状态都更新
            if(YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())){
                reservation.setTallyFlag(5);
                if(reservation.getCntrIdB()==null){
                    reservation.setTallyFlagB(5);
                }
                if (StrUtil.isBlank(reservation.getCntrNo())) {
                    reservation.setCntrNo(cntrNo);
                }
                //如果2个箱子最后一个箱子理货完成后才发送空车核放单
                if (reservation.getTallyFlag() == 5 && reservation.getTallyFlagB() == 5) {
                    istallEnd = true;
                }
            }else{
                reservation.setTallyFlag(10);
                if(reservation.getCntrIdB()==null){
                    reservation.setTallyFlagB(10);
                }
                if (StrUtil.isBlank(reservation.getCntrNo())) {
                    reservation.setCntrNo(cntrNo);
                }
                //如果2个箱子最后一个箱子理货完成后才发送空车核放单
                if (reservation.getTallyFlag() == 10 && reservation.getTallyFlagB() == 10) {
                    istallEnd = true;
                }
            }
        } else if(cntrBFlag) {
            //箱B理货
            if(YwOrderTypeEnum.TZHK.getCode().equals(reservation.getReservationType())){
                reservation.setTallyFlagB(15);
                if (StrUtil.isBlank(reservation.getCntrNoB())) {
                    reservation.setCntrNoB(cntrNo);
                }
                //如果2个箱子最后一个箱子理货完成后才发送空车核放单
                if (reservation.getTallyFlag() == 5 && reservation.getTallyFlagB() == 5) {
                    istallEnd = true;
                }
            }else{
                reservation.setTallyFlagB(10);
                if (StrUtil.isBlank(reservation.getCntrNoB())) {
                    reservation.setCntrNoB(cntrNo);
                }
                //如果2个箱子最后一个箱子理货完成后才发送空车核放单
                if (reservation.getTallyFlag() == 10 && reservation.getTallyFlagB() == 10) {
                    istallEnd = true;
                }
            }
        }
        if (ywOrder != null) {
            //判断用客户合同还是标准合同
            List<ChargeContract> chargeContracts = chargeContractMapper.selectList(new QueryWrapper<ChargeContract>().eq("disable", 0).le("begin_time", LocalDateTime.now()).ge("end_time", LocalDateTime.now())
                    .and(wrapper -> wrapper.eq("contract_name", "标准合同").or().eq("company_id", ywOrder.getCompanyId())));
            Long contractId=null;
            if (chargeContracts.size() == 1) {
                contractId=chargeContracts.get(0).getId();
            }else if (chargeContracts.size() > 1) {
                contractId=chargeContracts.stream().filter(s -> !"标准合同".equals(s.getContractName())).collect(Collectors.toList()).get(0).getId();
            }
            //生成计费流水
            List<ChargeConfig> configs=chargeConfigService.getListByName("提箱");
            if(!configs.isEmpty()){
                List<ChargeItemVo> chargeItemVos = chargeItemService.getChargeItemVoList(configs.stream().map(s -> String.valueOf(s.getChargeItemId())).collect(Collectors.joining(",")));
                List<CntrChargeDetail> cntrChargeDetails=new ArrayList<>();
                CompanyInfo companyInfo = companyInfoMapper.selectById(ywOrder.getCompanyId());
                //组装一个基础信息
                CntrChargeDetail cntrChargeDetail=new CntrChargeDetail();
                cntrChargeDetail.setYwOrderId(ywOrder.getId().toString());
                cntrChargeDetail.setCntrNo(ywOrderCntr.getCntrNo());
                cntrChargeDetail.setTruckNo(reservation.getCarNo());
                cntrChargeDetail.setGoodsName(ywOrderCntr.getGoodsName());
                cntrChargeDetail.setCntrSize(ywOrderCntr.getCntrSize());
                cntrChargeDetail.setEfFlag(ywOrderCntr.getEfFlag());
                cntrChargeDetail.setCntrNo(ywOrderCntr.getCntrNo());
                cntrChargeDetail.setBillNo(ywOrderCntr.getBillNo());
                cntrChargeDetail.setTallyTime(LocalDateTime.now());
                cntrChargeDetail.setCompanyId(companyInfo.getId().intValue());
                cntrChargeDetail.setCompanyName(companyInfo.getName());
                cntrChargeDetail.setOrderNo(ywOrder.getOrderNo());
                cntrChargeDetail.setCreateTime(LocalDateTime.now());
                cntrChargeDetails=configService.comCalculateCntrCost(chargeItemVos,cntrChargeDetail,contractId,companyInfo);
                cntrChargeDetailService.saveBatch(cntrChargeDetails);
            }
        }


        ywOrderCntrService.updateById(ywOrderCntr);
        if(ywOrderAJzx !=null){
            ywOrderCntrService.updateById(ywOrderAJzx);
        }
        if(ywOrderBJzx != null){
            ywOrderCntrService.updateById(ywOrderBJzx);
        }
        cntrInfo.setCntrWorkRemarkB(DdEnum.YTC.getName());
        cntrInfoMapper.updateById(cntrInfo);
        cntrLog.setCntrWorkRemarkB(DdEnum.YTC.getName());
        cntrLogMapper.insert(cntrLog);
        vehicleReservationMapper.updateById(reservation);
        //同时修改空回的预约信息
        if(khreservation!=null){
            vehicleReservationMapper.updateById(khreservation);
        }
        //修改原预约信息
        if(hxFlag) {
            vehicleReservationMapper.updateById(oldVehicleReservation);
        }
        if(hxFlag && kholdVehicleReservation!=null ) {
            vehicleReservationMapper.updateById(kholdVehicleReservation);
        }
        if (reservation.getCntrIdA() != null && reservation.getCntrNo() == null) {
            vehicleReservationService.update(Wrappers.<VehicleReservation>lambdaUpdate().eq(VehicleReservation::getId, reservation.getId())
                    .set(VehicleReservation::getCntrNo, null)
                    .set(VehicleReservation::getCntrIdA, null)
                    .set(VehicleReservation::getCntrSize, null));
        }
        if (reservation.getCntrIdB() != null && reservation.getCntrNoB() == null) {
            vehicleReservationService.update(Wrappers.<VehicleReservation>lambdaUpdate().eq(VehicleReservation::getId, reservation.getId())
                    .set(VehicleReservation::getCntrNoB, null).set(VehicleReservation::getCntrIdB, null)
                    .set(VehicleReservation::getCntrSizeB, null));
        }
        //自动出卡 提重业务不需要自动出卡
        if (istallEnd &&  !reservation.getReservationType().contains(YwOrderTypeEnum.TZ.getCode())) {
            List<DeclareCar> declareCars = baseMapper.selectList(new QueryWrapper<DeclareCar>().eq("disable", 0).eq("vehicle_id_b", declareCarVo.getId()));
            if(!declareCars.isEmpty()){
                DeclareCar declareCar = declareCars.get(0);
                if(declareCar.getVehicleId()!=null){
                    VehicleReservation reservation1 = vehicleReservationMapper.selectById(declareCar.getVehicleId());
                    if(reservation1.getTallyFlag()==10 && reservation1.getTallyFlagB() ==10){
                        txck(declareCar, reservation);
                    }
                }else{
                    txck(declareCar, reservation);
                }
            }
        }
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil<T> updateCntrTxTallyNew(DeclareCarVo declareCarVo) {
        String cntrNo = "";
        if (StrUtil.isNotBlank(declareCarVo.getCntrA())) {
            cntrNo = declareCarVo.getCntrA();
        } else if (StrUtil.isNotBlank(declareCarVo.getCntrB())) {
            cntrNo = declareCarVo.getCntrB();
        }
        if (StrUtil.isBlank(cntrNo)) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTRNO_NULL);
        }
        CntrInfo cntrInfo = cntrInfoMapper.selectOne(Wrappers.<CntrInfo>lambdaQuery()
                .eq(CntrInfo::getCntrNo, cntrNo));
        if (cntrInfo == null) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOT_EXIST);
        }
        if (cntrInfo.getStatus() != 0) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOTIN_YARD);
        }
        if (cntrInfo.getLockFlag() == 1) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_IS_LOCK);
        }
        //箱子区域贝位判断
        if (StringUtils.isNotEmpty(cntrInfo.getArea()) && cntrInfo.getArea().contains("区")){

        }else{
            //判断箱子上方是否有箱子
            if (upCntrCheck(cntrInfo.getBay())) {
                return ResultUtil.build(400, "贝位" + cntrInfo.getBay() + "上面存在箱子,请检查");
            }
        }

        DeclareCar declareCar = baseMapper.selectById(declareCarVo.getId());

        if(declareCar.getVehicleId()!=null){
            VehicleReservation reservation = vehicleReservationMapper.selectById(declareCar.getVehicleId());
            if(reservation!=null) {
                if (reservation.getTallyFlag() == 0 || reservation.getTallyFlagB() == 0) {
                    return ResultUtil.build(400, "该车辆未落箱,请检查");
                }
            }
        }

        YwOrderCntr ywOrderCntr = null;

        //提重
        ywOrderCntr = ywOrderCntrService.getOne(Wrappers.<YwOrderCntr>lambdaQuery()
                .eq(YwOrderCntr::getOrderNo, declareCarVo.getOrderNo())
                .eq(YwOrderCntr::getCntrNo, cntrNo));

        if (ywOrderCntr == null){
            return ResultUtil.build(400, "该车辆没有授权提箱操作！");
        }

        //显示提箱委托
        VehicleReservationVo reservationVo=new VehicleReservationVo();
        reservationVo.setCarNo(declareCarVo.getCarNo());
        List<YwOrder> txOrder=ywOrderMapper.getTxOrder(reservationVo);
        if(txOrder.size()>0){
            if(!(txOrder.stream().map(YwOrder::getOrderNo).collect(Collectors.toList()).contains(ywOrderCntr.getOrderNo()))){
                return ResultUtil.build(400, "该车辆没有授权提箱操作！");
            }
        }else{
            return ResultUtil.build(400, "该车辆没有授权提箱操作！");
        }

        if(ywOrderCntr == null){
            return ResultUtil.build(400, "箱清单信息不存在");
        }
        if (ywOrderCntr.getTallyState() != 0) {
            return ResultUtil.build(400, "箱子已理货,请勿重新操作");
        }

        //修改清单箱理货状态
        //业务中存在两次理货的数据,理货状态设置为第一次理货

        YwOrder ywOrder = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable",0).eq("order_no",ywOrderCntr.getOrderNo()));
        if(StringUtils.isEmpty(declareCar.getOrderNo())){
            declareCar.setOrderNo(ywOrder.getOrderNo());
            declareCar.setTxOrderNo(ywOrder.getOrderNo());
            declareCar.setYwcode(ywOrder.getOrderType());
        }else{
            declareCar.setTxOrderNo(ywOrder.getOrderNo());
        }

        List<String> codes = Collections.singletonList(YwOrderTypeEnum.TZHK.getCode());
        if (codes.contains(declareCar.getYwcode())) {
            ywOrderCntr.setTallyState(5);
            ywOrderCntr.setEfFlag("F");
        } else {
            ywOrderCntr.setTallyState(10);
        }
        //业务中存在两次理货的数据,第二次设置结束理货时间

        ywOrderCntr.setTallyTime(LocalDateTime.now());
        ywOrderCntr.setLhWeight(declareCarVo.getLhWeight());
        if (declareCarVo.getUserId() != null) {
            ywOrderCntr.setTallyUserId(declareCarVo.getUserId());
        }else if(declareCarVo.getMemberId() != null){
            ywOrderCntr.setTallyMemberId(declareCarVo.getMemberId());
        }
        ywOrderCntr.setTallyUserName(declareCarVo.getUserName());


        //判断箱子收货人 和 委托公司是否同一个
        if(cntrInfo.getIsZt()==null || cntrInfo.getIsZt()==0){
            if(StringUtils.isNotEmpty(ywOrderCntr.getPayer())){
                CompanyInfo companyIdByName = companyInfoMapper.getCompanyIdByName(ywOrderCntr.getPayer());
                if (companyIdByName !=null && !(companyIdByName.getName().equals(cntrInfo.getPayer()) || companyIdByName.getNameSimple().equals(cntrInfo.getPayer()))){
                    return ResultUtil.build(400, "集装箱付款人不匹配，无法提箱！");
                }
            }
        }

        //修改箱信息表状态
        cntrInfo.setBay("");
        cntrInfo.setArea("");
        cntrInfo.setStatus(1);
        cntrInfo.setPayer(ywOrderCntr.getPayer()); //付款人
        cntrInfo.setTrackOrCar(1);//汽车
        cntrInfo.setRemark("提箱理货");
        cntrInfo.setOperateType("TXLH");
        cntrInfo.setUserId(declareCarVo.getUserId());
        cntrInfo.setMemberId(declareCarVo.getMemberId());
        cntrInfo.setUserName(declareCarVo.getUserName());
        cntrInfo.setMachineName(declareCarVo.getMachineName());
        cntrInfo.setAccesspath(declareCarVo.getAccesspath());
        cntrInfo.setCarNo(declareCarVo.getCarNo());
        if(ywOrder != null && YwOrderTypeEnum.SGJ.getCode().equals(ywOrder.getOrderType())){
            cntrInfo.setLogTag("0");
        }
        cntrInfo.setOrderNo(cntrInfo.getOrderNo());
        cntrInfo.setCntrWorkRemarkB(DdEnum.YTC.getName());

        cntrInfo.setLockFlag(0);//散改集 提箱理货后解锁
        cntrInfo.setOutTime(LocalDateTime.now());
        cntrInfo.setLhWeight(declareCarVo.getLhWeight());
        //保存箱动态
        CntrLog cntrLog = new CntrLog();
        BeanUtils.copyProperties(cntrInfo, cntrLog);
        cntrLog.setCreatetime(LocalDateTime.now());
        if(ywOrder != null && YwOrderTypeEnum.SGJ.getCode().equals(ywOrder.getOrderType())) {
            cntrLog.setLockRemark("散改集提箱理货解锁");
        }
        cntrLog.setId(null);

        boolean istallEnd=true;

        if (ywOrder != null) {
            //判断用客户合同还是标准合同
            List<ChargeContract> chargeContracts = chargeContractMapper.selectList(new QueryWrapper<ChargeContract>().eq("disable", 0).le("begin_time", LocalDateTime.now()).ge("end_time", LocalDateTime.now())
                    .and(wrapper -> wrapper.eq("contract_name", "标准合同").or().eq("company_id", ywOrder.getCompanyId())));
            Long contractId=null;
            if (chargeContracts.size() == 1) {
                contractId=chargeContracts.get(0).getId();
            }else if (chargeContracts.size() > 1) {
                contractId=chargeContracts.stream().filter(s -> !"标准合同".equals(s.getContractName())).collect(Collectors.toList()).get(0).getId();
            }
            //生成计费流水
            List<ChargeConfig> configs=chargeConfigService.getListByName("提箱");
            if(!configs.isEmpty()){
                List<ChargeItemVo> chargeItemVos = chargeItemService.getChargeItemVoList(configs.stream().map(s -> String.valueOf(s.getChargeItemId())).collect(Collectors.joining(",")));
                List<CntrChargeDetail> cntrChargeDetails=new ArrayList<>();
                CompanyInfo companyInfo = companyInfoMapper.selectById(ywOrder.getCompanyId());
                //组装一个基础信息
                CntrChargeDetail cntrChargeDetail=new CntrChargeDetail();
                cntrChargeDetail.setYwOrderId(ywOrder.getId().toString());
                cntrChargeDetail.setCntrNo(ywOrderCntr.getCntrNo());
                cntrChargeDetail.setTruckNo(declareCarVo.getCarNo());
                cntrChargeDetail.setGoodsName(ywOrderCntr.getGoodsName());
                cntrChargeDetail.setCntrSize(ywOrderCntr.getCntrSize());
                cntrChargeDetail.setEfFlag(ywOrderCntr.getEfFlag());
                cntrChargeDetail.setCntrNo(ywOrderCntr.getCntrNo());
                cntrChargeDetail.setBillNo(ywOrderCntr.getBillNo());
                cntrChargeDetail.setTallyTime(LocalDateTime.now());
                cntrChargeDetail.setCompanyId(companyInfo.getId().intValue());
                cntrChargeDetail.setCompanyName(companyInfo.getName());
                cntrChargeDetail.setOrderNo(ywOrder.getOrderNo());
                cntrChargeDetail.setCreateTime(LocalDateTime.now());
                cntrChargeDetails=configService.comCalculateCntrCost(chargeItemVos,cntrChargeDetail,contractId,companyInfo);
                cntrChargeDetailService.saveBatch(cntrChargeDetails);
            }
        }


        ywOrderCntrService.updateById(ywOrderCntr);

        cntrInfo.setCntrWorkRemarkB(DdEnum.YTC.getName());
        cntrInfoMapper.updateById(cntrInfo);
        cntrLog.setCntrWorkRemarkB(DdEnum.YTC.getName());
        cntrLogMapper.insert(cntrLog);


        if (StrUtil.isNotBlank(declareCarVo.getCntrA())) {
            declareCar.setCntrA(ywOrderCntr.getCntrNo());
            declareCar.setTallyFlag(10);
            declareCar.setHwnameA(cntrInfo.getGoods());
            declareCar.setEfFlagA(cntrInfo.getEfFlag());
            declareCar.setCntrSizeA(cntrInfo.getCntrSize());
            //给 卸车计划 数据添加 车号
            TrainPlanDetail trainPlanDetail = trainPlanDetailMapper.selectOne(new QueryWrapper<TrainPlanDetail>().eq("cntr_no", declareCar.getCntrA()).eq("disable", 0)
                    .isNull("train_number").orderByDesc("createtime").last("limit 1"));
            if(trainPlanDetail!=null){
                trainPlanDetail.setTrainNumber(declareCar.getCarNo());
                trainPlanDetailMapper.updateById(trainPlanDetail);
            }
        }

        if (StrUtil.isNotBlank(declareCarVo.getCntrB())) {
            declareCar.setCntrB(ywOrderCntr.getCntrNo());
            declareCar.setTallyFlagB(10);
            declareCar.setHwnameB(cntrInfo.getGoods());
            declareCar.setEfFlagB(cntrInfo.getEfFlag());
            declareCar.setCntrSizeB(cntrInfo.getCntrSize());
            //给 卸车计划 数据添加 车号
            TrainPlanDetail trainPlanDetail = trainPlanDetailMapper.selectOne(new QueryWrapper<TrainPlanDetail>().eq("cntr_no", declareCar.getCntrB()).eq("disable", 0)
                    .isNull("train_number").orderByDesc("createtime").last("limit 1"));
            if(trainPlanDetail!=null){
                trainPlanDetail.setTrainNumber(declareCar.getCarNo());
                trainPlanDetailMapper.updateById(trainPlanDetail);
            }
        }


        if("F".equals(cntrInfo.getEfFlag()) || cntrInfo.getCntrSize().startsWith("4")){
            declareCar.setTallyFlagB(10);
        }

        updateById(declareCar);

        //自动出卡 提重业务不需要自动出卡
//        if (istallEnd &&  !declareCarVo.getYwcode().contains(YwOrderTypeEnum.TZ.getCode())) {
//
//        }
        return ResultUtil.ok();
    }

    private void txck(DeclareCar declareCar, VehicleReservation reservation) {
        //自动出卡
        baseMapper.update(new DeclareCar(), Wrappers.<DeclareCar>lambdaUpdate()
                .eq(DeclareCar::getDisable, 0)
                .eq(DeclareCar::getCarNo, declareCar.getCarNo())
                .eq(DeclareCar::getId, declareCar.getId())
                .eq(DeclareCar::getOutType, 0)
                .set(DeclareCar::getOutType, 1)
                .set(DeclareCar::getOutTime, LocalDateTime.now()));
        reservation.setEndState(1);
        vehicleReservationService.updateById(reservation);

        //集装箱出卡 日志
        if(StringUtils.isNotEmpty(reservation.getCntrNo())){
            CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(reservation.getCntrNo());
            cntrInfo1.setRemark("集装箱出卡");
            cntrInfo1.setCntrWorkRemarkB(DdEnum.YTC.getName());
            cntrInfoMapper.updateById(cntrInfo1);
            CntrLog cntrLog1 = new CntrLog();
            BeanUtils.copyProperties(cntrInfo1,cntrLog1);
            cntrLog1.setId(null);
            cntrLog1.setCreatetime(LocalDateTime.now());
            cntrLogMapper.insert(cntrLog1);
        }
        if(StringUtils.isNotEmpty(reservation.getCntrNoB())){
            CntrInfo cntrInfo1 = cntrInfoMapper.getCntrInfoById(reservation.getCntrNoB());
            cntrInfo1.setRemark("集装箱出卡");
            cntrInfo1.setCntrWorkRemarkB(DdEnum.YTC.getName());
            cntrInfoMapper.updateById(cntrInfo1);
            CntrLog cntrLog1 = new CntrLog();
            BeanUtils.copyProperties(cntrInfo1,cntrLog1);
            cntrLog1.setId(null);
            cntrLog1.setCreatetime(LocalDateTime.now());
            cntrLogMapper.insert(cntrLog1);
        }
    }

    public boolean upCntrCheck(String bay) {
        String[] bays = bay.split("-");
        String area = bays[0];
        String bay1 = bays[1];
        String bay2 = bays[2];
        String bay3 = bays[3];
        //上面一层
        StringBuilder up = new StringBuilder();

        up.append(area).append("-").append(bay1).append("-").append(bay2).append("-").append(Integer.parseInt(bay3) + 1);

        if (up.length() > 0) {
            //上面箱
            CntrInfo one = cntrInfoMapper.selectOne(Wrappers.<CntrInfo>lambdaQuery().eq(CntrInfo::getBay, up.toString())
                    .eq(CntrInfo::getDisable, 0).eq(CntrInfo::getStatus, 0).select(CntrInfo::getCntrNo, CntrInfo::getCntrSize, CntrInfo::getBay, CntrInfo::getEfFlag));

            return one != null;
        }
        return false;
    }

    @Override
    public DeclareCarVo getNotFinishedWeigh(DeclareCarVo vo) {
        QueryWrapper<DeclareCar> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.eq("out_type", 0));// 未完成过磅的
        queryWrapper.and(wrapper -> wrapper.eq("car_no", vo.getCarNo()));
        queryWrapper.and(wrapper -> wrapper.eq("disable", 0));
        List<DeclareCar> declareCars = baseMapper.selectList(queryWrapper);
        DeclareCarVo declareCarVo = new DeclareCarVo();
        if (!declareCars.isEmpty()) {
            DeclareCar declareCar = declareCars.get(0);
            BeanUtils.copyProperties(declareCar, declareCarVo);
            YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", declareCarVo.getOrderNo()).eq("cntr_no", declareCarVo.getCntrA()).eq("disable", 0));
            if (cntrA != null) {
                CntrInfo cntrInfoA = cntrInfoMapper
                        .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", cntrA.getCntrNo()));
                declareCarVo.setCntrSizeA(cntrA.getCntrSize());
                declareCarVo.setGoodsA(cntrA.getGoodsName());
                declareCarVo.setSealNoA(cntrA.getSealNo());
                declareCarVo.setDestinationA(cntrA.getDestination());
                if (cntrInfoA != null) {
                    declareCarVo.setBayA(cntrInfoA.getBay());
                }
            }

            YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", declareCarVo.getOrderNo()).eq("cntr_no", declareCarVo.getCntrB()).eq("disable", 0));
            if (cntrB != null) {
                CntrInfo cntrInfoB = cntrInfoMapper
                        .selectOne(new QueryWrapper<CntrInfo>().eq("cntr_no", cntrB.getCntrNo()));
                declareCarVo.setCntrSizeB(cntrB.getCntrSize());
                declareCarVo.setGoodsB(cntrB.getGoodsName());
                declareCarVo.setSealNoB(cntrB.getSealNo());
                declareCarVo.setDestinationB(cntrB.getDestination());
                if (cntrInfoB != null) {
                    declareCarVo.setBayB(cntrInfoB.getBay());
                }
            }
        } else {
            declareCarVo = null;
        }
        return declareCarVo;
    }

    @Override
    public Map<String, Object> getStatisticsData(DeclareCarVo vo) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Map<String, Long>> inData = baseMapper.selectInData(vo);
        Map<String, Map<String, Long>> outData = baseMapper.selectOutData(vo);
        List<String> month = new ArrayList<>();
        List<Long> in = new ArrayList<>();
        List<Long> out = new ArrayList<>();
        String m;
        for (int i = 1; i <= 12; i++) {
            m = i > 9 ? i + "月" : "0" + i + "月";
            month.add(m);
            in.add(inData.get(m) == null ? 0L : inData.get(m).get("carNum"));
            out.add(outData.get(m) == null ? 0L : outData.get(m).get("carNum"));
        }
        map.put("month", month);
        map.put("in", in);
        map.put("out", out);
        return map;
    }

    @Override
    public List<DeclareCarVo> getList(Page<DeclareCarVo> page, DeclareCarVo vo) {
        List<DeclareCarVo> declareCarVos = new ArrayList<>();
        IPage<DeclareCarVo> pageList = null;

        pageList = baseMapper.getDeclareCarPage(page, vo);
        for (DeclareCarVo declareCarVo : pageList.getRecords()) {
            TrainPlanDetail cntrA = detailMapper.selectOne(new QueryWrapper<TrainPlanDetail>()
                    .eq("plan_id", vo.getPlanId()).eq("cntr_no", vo.getCntrA()).eq("disable", 0));
            if (cntrA != null) {
                declareCarVo.setGoodsA(cntrA.getGoods());
            }
        }
        return pageList.getRecords();
    }

    @Override
    public ResultUtil<T> addSgjInGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = new DeclareCar();
        BeanUtils.copyProperties(vo, declareCar);
        declareCar.setCreateTime(LocalDateTime.now());
        declareCar.setPlanTime(LocalDateTime.now());
        declareCar.setDisable(0);
        List<CntrLog> cntrLogs=new ArrayList<>();
        if (vo.getYwOrderId() != null) {
            YwOrder ywOrder = ywOrderMapper.selectById(vo.getYwOrderId());
            // 查看车牌号是否存在不存在就新增
            List<CarInfo> carInfos = carInfoMapper
                    .selectList(new QueryWrapper<CarInfo>().eq("car_no", declareCar.getCarNo()));
            if (carInfos.isEmpty()) {
                CarInfo carInfo = new CarInfo();
                carInfo.setCarNo(declareCar.getCarNo());
                carInfo.setUserId(vo.getUserId());
                carInfo.setDisable(false);
                carInfo.setInoutCar(0);
                carInfoMapper.insert(carInfo);
            }
            // 修改清单表的预约状态
            YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrA()).eq("disable", 0));
            YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrB()).eq("disable", 0));
            List<YwOrderCntr> ywOrderCntrs = new ArrayList<>();
            if (cntrA != null) {
                cntrA.setDeclareFlag(1);
                cntrA.setJkWeight(vo.getJkWeight());
                cntrA.setSealNo(vo.getSealNoA());
                declareCar.setJzxIdA(cntrA.getId());
                cntrA.setGoodsName(vo.getHwnameA());
                ywOrderCntrs.add(cntrA);
                //集装箱进卡 保存日志
                CntrLog cntrLog = saveCntrLogMethod(vo,cntrA,vo.getXkA(),"集装箱进卡");
                cntrLog.setCntrWorkRemark(FyEnum.YJC.getName());
                cntrLogs.add(cntrLog);
                CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntrA.getCntrNo());
                if(cntrInfo!=null){
                    cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                    cntrInfoMapper.updateById(cntrInfo);
                }
            }
            if (cntrB != null) {
                cntrB.setDeclareFlag(1);
                cntrB.setJkWeight(vo.getJkWeight());
                cntrB.setSealNo(vo.getSealNoB());
                declareCar.setJzxIdB(cntrB.getId());
                cntrA.setGoodsName(vo.getHwnameB());
                ywOrderCntrs.add(cntrB);
                //集装箱进卡 保存日志
                CntrLog cntrLog = saveCntrLogMethod(vo,cntrB,vo.getXkB(),"集装箱进卡");
                cntrLog.setCntrWorkRemark(FyEnum.YJC.getName());
                cntrLogs.add(cntrLog);
                CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntrB.getCntrNo());
                if(cntrInfo!=null){
                    cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                    cntrInfoMapper.updateById(cntrInfo);
                }
            }

            if (!ywOrderCntrs.isEmpty()) {
                ywOrderCntrService.updateBatchById(ywOrderCntrs);
            }
            declareCar.setCompanyName(ywOrder.getCompanyName());
//            if(StringUtils.isAllEmpty(vo.getCntrA(),vo.getCntrB())){
//                declareCar.setYwcode("TK");
//            }else{
//                declareCar.setYwcode("ZJ");
//            }
        }

        boolean b = save(declareCar);
        cntrLogService.saveBatch(cntrLogs);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> addSgjOutGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = baseMapper.selectById(vo.getId());
        boolean b=false;
        //把同委托下的同辆车子全部出卡 就是
        List<DeclareCar> declareCars = baseMapper.selectList(new QueryWrapper<DeclareCar>().eq("disable", 0)
                .eq("order_no", declareCar.getOrderNo()).eq("car_no",declareCar.getCarNo()));
        if(declareCars.size()>0){
            for (DeclareCar car : declareCars) {
                car.setOutType(1);
                car.setOutTime(LocalDateTime.now());
            }
            b = updateBatchById(declareCars);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> addSgjNewInGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = baseMapper.selectById(vo.getId());
        declareCar.setCreateFlag(1);
        DeclareCar newDeclareCar = new DeclareCar();
        newDeclareCar.setCompanyName(declareCar.getCompanyName());
        newDeclareCar.setCarNo(declareCar.getCarNo());
        newDeclareCar.setPlanTime(LocalDateTime.now());
        newDeclareCar.setYwOrderId(declareCar.getYwOrderId());
        newDeclareCar.setOrderNo(declareCar.getOrderNo());
        newDeclareCar.setYwcode(YwOrderTypeEnum.TK.getCode());
        newDeclareCar.setEfFlagA("E");
        newDeclareCar.setEfFlagB("E");
        newDeclareCar.setCreateTime(LocalDateTime.now());
        updateById(declareCar);
        int i = baseMapper.insert(newDeclareCar);
        if(i>0){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public ResultUtil<T> addNewJkInGateRet(DeclareCarVo vo,String autoTally) {
        DeclareCar declareCar = new DeclareCar();
        BeanUtils.copyProperties(vo, declareCar);

        if(StringUtils.isEmpty(declareCar.getYwcode())){
            if(StringUtils.isNotEmpty(declareCar.getCntrA())){
                if("E".equals(declareCar.getEfFlagA())){
                    declareCar.setYwcode("KJ");
                }else{
                    declareCar.setYwcode("ZJ");
                }
            }
        }
        declareCar.setCreateTime(LocalDateTime.now());
        declareCar.setPlanTime(LocalDateTime.now());
        declareCar.setDisable(0);
        List<DeclareCar> declareCars = baseMapper.selectList(new QueryWrapper<DeclareCar>().eq("disable", 0).eq("car_no", vo.getCarNo()).eq("out_type", 0));
        if(declareCars.size()>0){
            return ResultUtil.build(400,"车辆已进卡,请检查!");
        }


        List<CntrLog> cntrLogs=new ArrayList<>();

        //判断箱A 箱B是否在箱信息表中
        if(StringUtils.isNotEmpty(declareCar.getCntrA())){
            CntrInfo cntrInfo = cntrInfoMapper.selectById(declareCar.getCntrA());
            if(cntrInfo==null){
                cntrInfo=new CntrInfo();
                cntrInfo.setCntrNo(declareCar.getCntrA());
                cntrInfo.setCntrSize(declareCar.getCntrSizeA());
                cntrInfo.setXk(declareCar.getXkA());
                cntrInfo.setEfFlag(declareCar.getEfFlagA());
                cntrInfo.setCreatetime(LocalDateTime.now());
                cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                cntrInfo.setAccesspath("PC端");
                cntrInfo.setCarNo(vo.getCarNo());
                cntrInfo.setUserId(vo.getUserId());
                cntrInfo.setUserName(vo.getUserName());
                cntrInfo.setKaWeight(vo.getJkWeight());
                //自动理货
                if("true".equals(autoTally)){
                    cntrInfo.setStatus(0);
                    cntrInfo.setBay("空箱区");
                    cntrInfo.setInTime(LocalDateTime.now());
                }else{
                    cntrInfo.setStatus(1);
                }
                cntrInfoMapper.insert(cntrInfo);

                CntrLog cntrLog=new CntrLog();
                BeanUtils.copyProperties(cntrInfo,cntrLog);
                cntrLog.setRemark("集装箱进卡");
                cntrLogs.add(cntrLog);
            }
        }

        if(StringUtils.isNotEmpty(declareCar.getCntrB())){
            CntrInfo cntrInfo = cntrInfoMapper.selectById(declareCar.getCntrB());
            if(cntrInfo==null){
                cntrInfo=new CntrInfo();
                cntrInfo.setCntrNo(declareCar.getCntrB());
                cntrInfo.setCntrSize(declareCar.getCntrSizeB());
                cntrInfo.setXk(declareCar.getXkB());
                cntrInfo.setEfFlag(declareCar.getEfFlagB());
                cntrInfo.setCreatetime(LocalDateTime.now());
                cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                cntrInfo.setAccesspath("PC端");
                cntrInfo.setCarNo(vo.getCarNo());
                cntrInfo.setUserId(vo.getUserId());
                cntrInfo.setUserName(vo.getUserName());
                cntrInfo.setKaWeight(vo.getJkWeight());
                //自动理货
                if("true".equals(autoTally)){
                    cntrInfo.setStatus(0);
                    cntrInfo.setBay("空箱区");
                    cntrInfo.setInTime(LocalDateTime.now());
                }else{
                    cntrInfo.setStatus(1);
                }
                cntrInfoMapper.insert(cntrInfo);

                CntrLog cntrLog=new CntrLog();
                BeanUtils.copyProperties(cntrInfo,cntrLog);
                cntrLog.setRemark("集装箱进卡");
                cntrLogs.add(cntrLog);
            }
        }

        // 查看车牌号是否存在不存在就新增
        List<CarInfo> carInfos = carInfoMapper
                .selectList(new QueryWrapper<CarInfo>().eq("car_no", declareCar.getCarNo()));
        if (carInfos.isEmpty()) {
            CarInfo carInfo = new CarInfo();
            carInfo.setCarNo(declareCar.getCarNo());
            carInfo.setUserId(vo.getUserId());
            carInfo.setDisable(false);
            carInfo.setInoutCar(0);
            carInfoMapper.insert(carInfo);
        }

        if (StringUtils.isNotEmpty(vo.getOrderNo())) {
            YwOrder ywOrder = ywOrderMapper.selectOne(new QueryWrapper<YwOrder>().eq("disable",0).eq("order_no",vo.getOrderNo()));
            declareCar.setYwOrderId(ywOrder.getId());

            // 修改清单表的预约状态
            YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrA()).eq("disable", 0));
            YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                    .eq("order_no", ywOrder.getOrderNo()).eq("cntr_no", declareCar.getCntrB()).eq("disable", 0));
            List<YwOrderCntr> ywOrderCntrs = new ArrayList<>();
            if (cntrA != null) {
                if(StringUtils.isNotEmpty(declareCar.getCntrA()) && !cntrA.getCntrNo().equals(declareCar.getCntrA())){
                    pushService.pushMsgToAll("进卡集装箱A箱号改变");
                }
                if(StringUtils.isNotEmpty(declareCar.getCntrSizeA()) && !cntrA.getCntrSize().equals(declareCar.getCntrSizeA())){
                    pushService.pushMsgToAll("进卡集装箱A箱型改变");
                    cntrA.setCntrSize(declareCar.getCntrSizeA());
                }
                cntrA.setDeclareFlag(1);
                cntrA.setJkWeight(vo.getJkWeight());
                cntrA.setSealNo(vo.getSealNoA());
                declareCar.setJzxIdA(cntrA.getId());
                ywOrderCntrs.add(cntrA);
                //集装箱进卡 保存日志
                if(!("TZ".equals(ywOrder.getOrderType()))) {
                    CntrLog cntrLog = saveCntrLogMethod(vo,cntrA,vo.getXkA(),"集装箱进卡");
                    cntrLog.setCntrWorkRemark(FyEnum.YJC.getName());
                    CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntrA.getCntrNo());
                    cntrInfo.setKaWeight(vo.getJkWeight());
                    if(("HK".equals(ywOrder.getOrderType()) || "KJ".equals(ywOrder.getOrderType())) && "true".equals(autoTally)){
                        cntrLog.setStatus(0);
                        cntrLog.setBay("空箱区");
                        cntrLog.setInTime(LocalDateTime.now());
                        cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                        cntrInfo.setStatus(0);
                        cntrInfo.setBay("空箱区");
                        cntrInfo.setKaWeight(vo.getJkWeight());
                        cntrInfo.setInTime(LocalDateTime.now());
                    }
                    cntrInfoMapper.updateById(cntrInfo);
                    cntrLogs.add(cntrLog);
                }
            }
            if (cntrB != null) {
                if(StringUtils.isNotEmpty(declareCar.getCntrB()) && !cntrB.getCntrNo().equals(declareCar.getCntrB())){
                    pushService.pushMsgToAll("进卡集装箱B箱号改变");
                }
                if(StringUtils.isNotEmpty(declareCar.getCntrSizeB()) && !cntrB.getCntrSize().equals(declareCar.getCntrSizeB())){
                    pushService.pushMsgToAll("进卡集装箱B箱型改变");
                    cntrB.setCntrSize(declareCar.getCntrSizeB());
                }
                cntrB.setDeclareFlag(1);
                cntrB.setJkWeight(vo.getJkWeight());
                cntrB.setSealNo(vo.getSealNoB());
                declareCar.setJzxIdB(cntrB.getId());
                ywOrderCntrs.add(cntrB);
                //集装箱进卡 保存日志
                if(!("TZ".equals(ywOrder.getOrderType()))) {
                    CntrLog cntrLog = saveCntrLogMethod(vo,cntrB,vo.getXkB(),"集装箱进卡");
                    cntrLog.setCntrWorkRemark(FyEnum.YJC.getName());

                    CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntrB.getCntrNo());
                    cntrInfo.setKaWeight(vo.getJkWeight());
                    if(("HK".equals(ywOrder.getOrderType()) || "KJ".equals(ywOrder.getOrderType())) && "true".equals(autoTally)){
                        cntrLog.setStatus(0);
                        cntrLog.setBay("空箱区");
                        cntrLog.setInTime(LocalDateTime.now());
                        cntrInfo.setCntrWorkRemark(FyEnum.YJC.getName());
                        cntrInfo.setStatus(0);
                        cntrInfo.setBay("空箱区");
                        cntrInfo.setInTime(LocalDateTime.now());
                    }
                    cntrInfoMapper.updateById(cntrInfo);
                    cntrLogs.add(cntrLog);
                }

            }

            if (!ywOrderCntrs.isEmpty()) {
                ywOrderCntrService.updateBatchById(ywOrderCntrs);
            }
            declareCar.setCompanyName(ywOrder.getCompanyName());

            if(ywOrder.getOrderType().equals("TZHK")){
                if(cntrA!=null && cntrA.getTallyState()==5){
                    declareCar.setYwcode("KJ");
                }
            }else{
                declareCar.setYwcode(ywOrder.getOrderType());
            }

        }
        boolean b = save(declareCar);

        List<String> ywcodes=new ArrayList<>();
        ywcodes.add(YwOrderTypeEnum.TZHK.getCode());
        ywcodes.add(YwOrderTypeEnum.TZ.getCode());
        if(StringUtils.isNotEmpty(declareCar.getCntrA()) && (StringUtils.isEmpty(declareCar.getYwcode()) || !(ywcodes.contains(declareCar.getYwcode())))){
            DeclareCarVo declareCarVo=new DeclareCarVo();
            declareCarVo.setCarNo(declareCar.getCarNo());
            List<DeclareCarVo> txCarList = isTxcarList(declareCarVo);
            if(txCarList.size()>0){
                DeclareCar declareCar1=new DeclareCar();
                DeclareCarVo declareCarVo1 = txCarList.get(0);
                declareCar1.setCarNo(declareCarVo1.getCarNo());
                declareCar1.setPid(declareCar.getId());
                declareCar1.setPlanTime(declareCar.getPlanTime());
                declareCar1.setCreateTime(declareCar.getCreateTime());
                declareCar1.setCompanyName(declareCarVo1.getCompanyName());
                save(declareCar1);
            }
        }
        if(cntrLogs.size()>0){
            cntrLogService.saveBatch(cntrLogs);
        }

        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    private List<DeclareCarVo> isTxcarList(DeclareCarVo declareCarVo) {
        List<DeclareCarVo> list =baseMapper.isTxcarList(declareCarVo);
        return list;
    }

    @Override
    public ResultUtil<T> addNewCkInGateRet(DeclareCarVo vo) {
        DeclareCar declareCar = baseMapper.selectById(vo.getId());
        vo.setCreateTime(declareCar.getCreateTime());
        vo.setPlanTime(declareCar.getPlanTime());
        vo.setOutType(1);
        vo.setOutTime(LocalDateTime.now());
        if (StrUtil.isBlank(vo.getCntrA())) {
            vo.setCntrA(declareCar.getCntrA());
        }
        if (StrUtil.isBlank(vo.getCntrB())) {
            vo.setCntrB(declareCar.getCntrB());
        }
        DeclareCar declareCar1=new DeclareCar();
        BeanUtils.copyProperties(vo, declareCar1);
        declareCar1.setTallyFlag(10);
        declareCar1.setTallyFlagB(10);
        declareCar1.setPid(declareCar.getPid());
        int i = baseMapper.updateById(declareCar1);

        //出卡提箱的车
        if(declareCar1.getPid()!=null){
            DeclareCar declareCar2 = baseMapper.selectById(declareCar1.getPid());
            if(declareCar2!=null){
                if(declareCar2.getTallyFlag()==0 && declareCar2.getTallyFlagB()==0){
                    declareCar2.setDisable(1);
                    updateById(declareCar2);
                }else{
                    declareCar2.setOutType(1);
                    declareCar2.setOutTime(LocalDateTime.now());
                    updateById(declareCar2);
                }
            }
        }


        YwOrderCntr cntrA = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", declareCar.getOrderNo()).eq("cntr_no", declareCar.getCntrA()).eq("disable", 0));
        YwOrderCntr cntrB = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", declareCar.getOrderNo()).eq("cntr_no", declareCar.getCntrB()).eq("disable", 0));
        // 修改清单表的预约状态
        List<YwOrderCntr> trainPlanCntrs = new ArrayList<>();
        if (cntrA != null) {
            if(declareCar.getTallyFlag()==0){
                cntrA.setOrderFlag(0);
                cntrA.setDeclareFlag(0);
            }else{
                cntrA.setOrderFlag(1);
            }
            cntrA.setCkWeight(vo.getCkWeight());
            cntrA.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())
                    .subtract(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())).toString());

            trainPlanCntrs.add(cntrA);
            ywOrderCntrMapper.updateById(cntrA);
            if(cntrA.getOrderNo().indexOf("TZ") != -1){
                CntrInfo cntrInfo = cntrInfoMapper.getCntrInfoById(cntrA.getCntrNo());
                if(cntrInfo!=null){
                    cntrInfo.setRemark("集装箱出卡");
                    cntrInfo.setCntrWorkRemarkB(DdEnum.YTC.getName());
                    cntrInfoMapper.updateById(cntrInfo);
                    CntrLog cntrLog = new CntrLog();
                    BeanUtils.copyProperties(cntrInfo,cntrLog);
                    cntrLog.setId(null);
                    cntrLog.setCreatetime(LocalDateTime.now());
                    cntrLogMapper.insert(cntrLog);
                }
            }
        }
        if (cntrB != null) {
            if(declareCar.getTallyFlagB()==0){
                cntrB.setOrderFlag(0);
                cntrB.setDeclareFlag(0);
            }else{
                cntrA.setOrderFlag(1);
            }
            cntrB.setCkWeight(vo.getCkWeight());
            cntrB.setWeight(new BigDecimal(StringUtils.isEmpty(vo.getCkWeight()) ? "0":vo.getCkWeight())
                    .subtract(new BigDecimal(StringUtils.isEmpty(vo.getJkWeight()) ? "0":vo.getJkWeight())).toString());

            trainPlanCntrs.add(cntrB);
            ywOrderCntrMapper.updateById(cntrB);
        }
        if(i>0){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public List<DeclareCarVo> getCkCarNo(DeclareCarVo vo) {
        List<DeclareCarVo> list= baseMapper.getCkCarNo(vo);
        List<DeclareCarVo> declareCarVos=new ArrayList<>();
        for (DeclareCarVo declareCarVo : list) {
            if(StrUtil.isNotBlank(declareCarVo.getYwcode()) && "SGJ".equals(declareCarVo.getYwcode())){
                if(declareCarVo.getTallyFlag()==10 && declareCarVo.getDcTallyFlag() ==10){
                    declareCarVos.add(declareCarVo);
                }
            }else{
                boolean b=true;
                if(StrUtil.isNotBlank(declareCarVo.getCntrNoA())){
                    if(declareCarVo.getTallyFlagA()==0){
                        b=false;
                    }
                }
                if(StrUtil.isNotBlank(declareCarVo.getCntrNoB())){
                    if(declareCarVo.getTallyFlagB()==0){
                        b=false;
                    }
                }
                if(StrUtil.isNotBlank(declareCarVo.getCntrNoC())){
                    if(declareCarVo.getTallyFlagC()==0){
                        b=false;
                    }
                }
                if(StrUtil.isNotBlank(declareCarVo.getCntrNoD())){
                    if(declareCarVo.getTallyFlagD()==0){
                        b=false;
                    }
                }
                if(b){
                    declareCarVos.add(declareCarVo);
                }
            }
        }
        return declareCarVos;
    }

    @Override
    public ResultUtil<T> backCntrTally(DeclareCarVo declareCarVo) {
        DeclareCar declareCar = baseMapper.selectById(declareCarVo.getId());

        CntrInfo cntrInfo = cntrInfoMapper.selectById(declareCarVo.getCntrNo());
        cntrInfo.setBay(declareCarVo.getBay());
        cntrInfo.setArea(declareCarVo.getBay().split("-")[0]);
        cntrInfo.setStatus(0);
        cntrInfo.setMachineName(declareCarVo.getMachineName());
        cntrInfo.setCntrWorkRemarkB(DdEnum.YSL.getName());
        cntrInfo.setRemark("退箱");

        CntrLog cntrLog=new CntrLog();
        BeanUtils.copyProperties(cntrInfo,cntrLog);
        cntrLog.setCreatetime(LocalDateTime.now());
        cntrLog.setUserName(declareCarVo.getUserName());

        YwOrderCntr cntr = ywOrderCntrMapper.selectOne(new QueryWrapper<YwOrderCntr>()
                .eq("order_no", declareCarVo.getTxOrderNo()).eq("cntr_no", declareCarVo.getCntrNo()).eq("disable", 0));
        cntr.setTallyState(0);
        cntr.setTallyTime(null);
        cntr.setTallyUserName("");

        if(declareCar.getOrderNo().equals(declareCar.getTxOrderNo())){
            if(StringUtils.isNotEmpty(declareCarVo.getCntrA()) && StringUtils.isEmpty(declareCar.getCntrB())){
                declareCar.setOrderNo("");
                declareCar.setTxOrderNo("");
            }
            if(StringUtils.isNotEmpty(declareCarVo.getCntrB()) && StringUtils.isEmpty(declareCar.getCntrA())){
                declareCar.setOrderNo("");
                declareCar.setTxOrderNo("");
            }
        }else{
            if(StringUtils.isNotEmpty(declareCarVo.getCntrA()) && StringUtils.isEmpty(declareCar.getCntrB())){
                declareCar.setTxOrderNo("");
            }
            if(StringUtils.isNotEmpty(declareCarVo.getCntrB()) && StringUtils.isEmpty(declareCar.getCntrA())){
                declareCar.setTxOrderNo("");
            }
        }

        if(StringUtils.isNotEmpty(declareCarVo.getCntrA())){
            declareCar.setCntrA("");
            declareCar.setTallyFlag(0);
            declareCar.setHwnameA("");
            declareCar.setEfFlagA("");
            declareCar.setCntrSizeA("");
            if(StringUtils.isEmpty(declareCar.getCntrB())){
                declareCar.setTallyFlagB(0);
            }
        }

        if(StringUtils.isNotEmpty(declareCarVo.getCntrB())){
            declareCar.setCntrB("");
            declareCar.setTallyFlagB(0);
            declareCar.setHwnameB("");
            declareCar.setEfFlagB("");
            declareCar.setCntrSizeB("");
            if(StringUtils.isEmpty(declareCar.getCntrA())){
                declareCar.setTallyFlag(0);
            }
        }


        boolean b = updateById(declareCar);
        if(b){
            cntrInfoMapper.updateById(cntrInfo);
            cntrLogMapper.insert(cntrLog);
            ywOrderCntrMapper.updateById(cntr);
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public List<DeclareCarVo> getTxCarList(DeclareCarVo vo) {
        List<DeclareCarVo> list =baseMapper.getTxCarList(vo);
        return list;
    }

    @Override
    public Map getCntrByOrderNo(YwOrderCntrVo vo) {
        List<YwOrderCntrVo> cntrByOrderNo = ywOrderCntrMapper.getCntrByOrderNo(vo);
        Map map=new HashMap();
        if(cntrByOrderNo.size()>0){
//            for (YwOrderCntrVo ywOrderCntrVo : cntrByOrderNo) {
//                if(StringUtils.isEmpty(ywOrderCntrVo.getJhBay())){
//                    if(StringUtils.isNotEmpty(ywOrderCntrVo.getJhArea()) && ywOrderCntrVo.getJhArea().matches(".*[\u4e00-\u9fa5]+.*")){
//                        ywOrderCntrVo.setJhBay(ywOrderCntrVo.getJhArea());
//                    }
//                }
//            }

            List<YwOrderCntrVo> aList = cntrByOrderNo.stream().filter(s -> StringUtils.isNotEmpty(s.getJhBay()))
                    .collect(Collectors.toList());
            if(aList.size()>0){
                for (YwOrderCntrVo ywOrderCntrVo : aList) {
//                    if(ywOrderCntrVo.getJhBay().indexOf("A") != -1){
                        String[] split = ywOrderCntrVo.getJhBay().split("-");
                        if("E".equals(ywOrderCntrVo.getEfFlag())){
                            map.put(split[0]+split[1]+"-E",map.get(split[0]+split[1]+"-E") !=null ? (Integer)map.get(split[0]+split[1]+"-E")+1:1);
                        }else{
                            map.put(split[0]+split[1]+"-F",map.get(split[0]+split[1]+"-F") !=null ? (Integer)map.get(split[0]+split[1]+"-F")+1:1);
                        }
//                    }else{
//                        if("E".equals(ywOrderCntrVo.getEfFlag())){
//                            map.put(ywOrderCntrVo.getJhBay()+"-E",map.get(ywOrderCntrVo.getJhBay()+"-E") !=null ? (Integer)map.get(ywOrderCntrVo.getJhBay()+"-E")+1:1);
//                        }else{
//                            map.put(ywOrderCntrVo.getJhBay()+"-F",map.get(ywOrderCntrVo.getJhBay()+"-F") !=null ? (Integer)map.get(ywOrderCntrVo.getJhBay()+"-F")+1:1);
//                        }
//
//                    }
                }
            }
        }
        Map newMap=new HashMap();
        map.forEach((k,v) -> {
            String kString = k.toString();
            String[] split = kString.split("-");
            if(split[1].equals("E")){
                newMap.put(split[0],newMap.get(split[0])!=null ? newMap.get(split[0])+"  空箱"+v+"个":"空箱"+v+"个");
            }else{
                newMap.put(split[0],newMap.get(split[0])!=null ? newMap.get(split[0])+"  重箱"+v+"个":"重箱"+v+"个");
            }
        });
        return newMap;
    }

    @Override
    public List<YwOrderCntrVo> getCntr(YwOrderCntrVo vo) {
        List<YwOrderCntrVo> cntrByOrderNo = ywOrderCntrMapper.getCntr(vo);
        return cntrByOrderNo;
    }

    @Override
    public ResultUtil<T> cancelTxRet(DeclareCarVo declareCarVo) {
        DeclareCar declareCar = baseMapper.selectById(declareCarVo.getId());
        declareCar.setOutType(1);
        declareCar.setOutTime(LocalDateTime.now());
        int i = baseMapper.updateById(declareCar);
        if(i>0){
            return ResultUtil.ok();
        }else{
            return ResultUtil.fail();
        }
    }

    @Override
    public List<DeclareCarVo> getCarNoJk(DeclareCarVo car) {
        List<DeclareCarVo> list = baseMapper.getTxCarList(car);//提箱进卡车辆
        List<DeclareCarVo> collect = new ArrayList<>();
        if(StringUtils.isEmpty(car.getOrderNo())){
            //pad
            collect = list.stream().filter(s -> s.getTallyFlag() == 0).collect(Collectors.toList());
        }else{
            //pc
            collect = list.stream().filter(s -> s.getTallyFlag() == 0 && s.getOrderNos().contains(car.getOrderNo())).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public List<DeclareCarVo> getLxCarList(DeclareCarVo vo) {
        List<DeclareCarVo> list =baseMapper.getLxCarList(vo);
        return list;
    }

    @Override
    public List<YwOrderCntrVo> getTzhkCntrList(DeclareCarVo vo) {
        //返回前端结果集
        List<YwOrderCntrVo> result = new ArrayList<>();
        //根据vo内的car_no获取此车的order_no集合orderNos
        List<DeclareCarVo> list = baseMapper.getTxCarList(vo);
        if (list.size() > 0) {
            DeclareCarVo vo1 = list.get(0);
            String orderNoList = vo1.getOrderNos();
            String[] orderNoArr = orderNoList.split(",");
            for (String orderNo : orderNoArr) {
                YwOrderCntrVo cntrVo = new YwOrderCntrVo();
                cntrVo.setOrderNo(orderNo);
                //根据YwOrderCntrVo的order_no号查询符合的箱号
                List<YwOrderCntrVo> cntrByOrderNo = ywOrderCntrMapper.getCntrByOrderNo(cntrVo);
                if (cntrByOrderNo.size() > 0) {
                    for (YwOrderCntrVo ywOrderCntrVo : cntrByOrderNo) {
                        if (StringUtils.isEmpty(ywOrderCntrVo.getJhBay())) {
                            if (StringUtils.isNotEmpty(ywOrderCntrVo.getJhArea()) && ywOrderCntrVo.getJhArea().matches(".*[\u4e00-\u9fa5]+.*")) {
                                ywOrderCntrVo.setJhBay(ywOrderCntrVo.getJhArea());
                            }
                        }
                    }
                    List<YwOrderCntrVo> aList = cntrByOrderNo.stream().filter(s -> StringUtils.isNotEmpty(s.getJhBay())).collect(Collectors.toList());
                    result.addAll(aList);
                }
            }
        } else {
            return null;
        }
        return result;
    }

    @Override
    public List<DeclareCar> getUnfinishedDeclareCarList(String orderNo) {
        List<DeclareCar> list = new ArrayList<DeclareCar>();
        QueryWrapper<DeclareCar> queryWrapper = new QueryWrapper<DeclareCar>();
        queryWrapper.eq("tally_flag", 0);
        queryWrapper.eq("disable", 0);
        if (StrUtil.isNotBlank(orderNo)) {
            queryWrapper.eq("order_no", orderNo);
        }

        queryWrapper.orderByAsc("jh_baya");
        list = baseMapper.selectList(queryWrapper);
        return list;
    }

    public Boolean checkCntrSize(YwOrderCntr ywOrderCntr,CntrInfo cntrInfo){
        String fsizeOne = "";
        String fsizeTwo = "";
        if(StrUtil.isNotBlank(ywOrderCntr.getCntrSize())){
            fsizeOne = ywOrderCntr.getCntrSize().substring(0,1);
        }
        if(StrUtil.isNotBlank(cntrInfo.getCntrSize())){
            fsizeTwo = cntrInfo.getCntrSize().substring(0,1);
        }
        if(StrUtil.isNotBlank(fsizeOne) && StrUtil.isNotBlank(fsizeTwo) && !fsizeOne.equals(fsizeTwo)){
            return false;
        }else {
            return true;
        }
    }

    private static CntrLog saveCntrLogMethod(DeclareCarVo vo, YwOrderCntr cntr,String xk,String remark) {
        CntrLog cntrLog=new CntrLog();
        cntrLog.setCntrNo(cntr.getCntrNo());
        cntrLog.setXk(xk);
        cntrLog.setCntrSize(cntr.getCntrSize());
        if(StrUtil.isNotBlank(cntr.getGoodsName())){
            cntrLog.setGoods(cntr.getGoodsName());
        }
        //如果不是空箱
        if(cntr.getWeight()!=null){
            cntrLog.setWeightPred(Double.valueOf(cntr.getWeight()));
        }
        cntrLog.setCompanyId(cntr.getCompanyId());
        cntrLog.setEfFlag(cntr.getEfFlag());
        cntrLog.setStatus(1);
        cntrLog.setAccesspath("PC端");
        cntrLog.setCarNo(vo.getCarNo());
        cntrLog.setRemark(remark);
        cntrLog.setUserId(vo.getUserId());
        cntrLog.setUserName(vo.getUserName());
        cntrLog.setCreatetime(LocalDateTime.now());
        cntrLog.setOrderNo(cntr.getOrderNo());
        return cntrLog;
    }
}
